package bookmanagementsystem.gui;

import bookmanagementsystem.dao.UserDAO;
import bookmanagementsystem.model.User;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ModifyUserInfoFrame extends JFrame {
    private JTextField oldPasswordField;
    private JTextField newPasswordField;
    private JTextField confirmNewPasswordField;
    private JButton saveButton;

    private UserDAO userDAO;
    private String currentUserID;

    public ModifyUserInfoFrame(String currentUserID) {
        this.currentUserID = currentUserID;
        userDAO = new UserDAO();

        setTitle("修改密码");
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        initComponents();
        addComponentsToFrame();
        addActionListeners();
    }

    private void initComponents() {
        oldPasswordField = new JTextField(20);
        newPasswordField = new JTextField(20);
        confirmNewPasswordField = new JTextField(20);
        saveButton = new JButton("保存修改");
    }

    private void addComponentsToFrame() {
        JPanel panel = new JPanel(new GridLayout(4, 2));

        panel.add(new JLabel("旧密码："));
        panel.add(oldPasswordField);
        panel.add(new JLabel("新密码："));
        panel.add(newPasswordField);
        panel.add(new JLabel("确认新密码："));
        panel.add(confirmNewPasswordField);
        panel.add(saveButton);

        getContentPane().add(panel);
    }

    private void addActionListeners() {
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String oldPassword = oldPasswordField.getText();
                String newPassword = newPasswordField.getText();
                String confirmNewPassword = confirmNewPasswordField.getText();

                // 验证新密码是否一致
                if (!newPassword.equals(confirmNewPassword)) {
                    JOptionPane.showMessageDialog(ModifyUserInfoFrame.this, "新密码与确认新密码不一致，请重新输入。");
                    return;
                }

                // 获取当前用户信息
                User currentUser = userDAO.getUserById(currentUserID);
                if (currentUser == null) {
                    JOptionPane.showMessageDialog(ModifyUserInfoFrame.this, "获取当前用户信息失败，请联系管理员。");
                    return;
                }

                // 验证旧密码是否正确
                if (!currentUser.getPassword().equals(oldPassword)) {
                    JOptionPane.showMessageDialog(ModifyUserInfoFrame.this, "旧密码输入错误，请重新输入。");
                    return;
                }

                currentUser.setPassword(newPassword);
                if (userDAO.updateUserPassword(currentUser)) {
                    JOptionPane.showMessageDialog(ModifyUserInfoFrame.this, "密码修改成功！");
                    dispose(); // 关闭当前修改信息窗口
                } else {
                    JOptionPane.showMessageDialog(ModifyUserInfoFrame.this, "密码修改失败，请联系管理员。");
                }
            }
        });
    }
}